#include <cxxtest/TestListener.h>
#include <cxxtest/TestTracker.h>
#include <cxxtest/TestRunner.h>
#include <cxxtest/RealDescriptions.h>
#include <cxxtest/ErrorPrinter.h>
#include "ViewerProtocol.h"
#include "ViewerProtocolFactory.h"
#include "Server.h"
#include "Client.h"
#include "Viewer.h"
#include <stdio.h>

//class Server;
//class Client;
//class Viewer;

class NetworkTest: public CxxTest::TestSuite
{
public:
	float someValue;
	std::string str;
	Server *server;
	Client *client;
	Viewer *viewer;
	QString protocol;
	static ElementData *edata;

	void setUp()
	{
		protocol = QString("SocketTcp");
		server = new Server(ViewerProtocolFactory::create(protocol));
		viewer = new Viewer(ViewerProtocolFactory::create(protocol));
		edata = new ElementData(NoType);
	}
	void tearDown()
	{
		delete server;
		delete viewer;
		delete edata;
		sleep(1);
	}

	void test1()
	{
		server->start();
		viewer->start();
		sleep(2);
		server->terminate();
		viewer->terminate();
		TS_ASSERT_EQUALS(viewer->errorCount, 0);
		TS_ASSERT_EQUALS(server->errorCount, 0);
		puts("Test 1 done...");
	}

	void test2()
	{
		server->stage = 1;
		viewer->stage = 1;
		server->start();
		viewer->start();
		sleep(10);
		TS_ASSERT_EQUALS(viewer->errorCount, 0);
		TS_ASSERT_EQUALS(server->errorCount, 0);
		puts("Test 2 done...");
	}

	void test3()
	{
		TS_ASSERT_EQUALS(viewer,(void*)NULL);
		return;
		//this test always fails
		server->stage = 2;
		viewer->stage = 2;
		server->start();
		viewer->start();
		sleep(10);
		TS_ASSERT_EQUALS(viewer->errorCount, 0);
		TS_ASSERT_EQUALS(server->errorCount, 0);
		puts("Test 3 done...");
	}

};
